//
//  SectionsUtilizationBar.m
//  budgetplan
//
//  Created by Barak on 10/26/12.
//  Copyright (c) 2012 Barak. All rights reserved.
//

#import "SectionsUtilizationBar.h"
#import <QuartzCore/QuartzCore.h>
#import "UIColor+Hex.h"


@implementation SectionsUtilizationBar

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code
        _sectionsColors = @[[UIColor colorWithCSS:@"#DDDF0D"],
                            [UIColor colorWithCSS:@"#55BF3B"],
                            [UIColor colorWithCSS:@"#DF5353"],
                            [UIColor colorWithCSS:@"#7798BF"],
                            [UIColor colorWithCSS:@"#aaeeee"],
                            [UIColor colorWithCSS:@"#ff0066"],
                            [UIColor colorWithCSS:@"#eeaaee"],
                            [UIColor colorWithCSS:@"#55BF3B"],
                            [UIColor colorWithCSS:@"#DF5353"],
                            [UIColor colorWithCSS:@"#7798BF"],
                            [UIColor colorWithCSS:@"#aaeeee"]];
        
        self.clipsToBounds = YES;
        self.backgroundColor = [UIColor blackColor];
        self.layer.cornerRadius = 10;
        
        UIImageView* background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradient"]];
        gloss = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gloss"]];
        [self addSubview:background];
        _animationTime = 2;
    }
    return self;
}

- (void) clear {
    if (_sectionViews){
        for (UIView* sectionView in _sectionViews) {
            [sectionView removeFromSuperview];
        }
    }
}


- (void) createSectionViews{
    
    if (_sectionViews){
        
        [self clear];
    } else {
        _sectionViews = [[NSMutableArray alloc] init];
    }
    
    [gloss removeFromSuperview];    
    int i= 0;
    int firstX = -0.5 * self.frame.size.width;
    int sectionInitiaWidth = 2;
    for (NSNumber* value in self.sectionsValues) {
        
        UIView* sectionView = [[UIView alloc] initWithFrame:CGRectMake(firstX + i*sectionInitiaWidth, 0, sectionInitiaWidth, self.frame.size.height)];
        
        sectionView.backgroundColor = [_sectionsColors objectAtIndex:i];
        [_sectionViews addObject:sectionView];
                                       
        [self addSubview:sectionView];
        i++;
    }
    
    gloss.frame = CGRectMake(firstX, 0, firstX + i*sectionInitiaWidth, gloss.frame.size.height);
    
    [self addSubview:gloss];
}

- (void) update {
    
    if (!_sectionViews || _sectionViews.count == 0){
        [self createSectionViews];
    }
    
    [UIView animateWithDuration:_animationTime delay:0 options:UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveEaseOut animations:^{
      
        CGFloat currX = 0;
        CGFloat barWidth = self.frame.size.width;
        CGFloat barHeight = self.frame.size.height;
        int i=0;
        for (UIView* sectionView in _sectionViews) {
            NSNumber* sectionValue = [_sectionsValues objectAtIndex:i];
            float sectionWidth = barWidth * ([sectionValue floatValue] / _total);
            sectionView.frame = CGRectMake(currX, 0, sectionWidth, barHeight);
            currX += sectionWidth;
            i++;
        }
        
        gloss.frame = CGRectMake(0, 0, currX, gloss.frame.size.height);

        
    } completion:^(BOOL finished) {
    }];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
